package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysLog;
import com.powernode.model.Result;
import com.powernode.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName SysLogController
 * @Description 系统操作日志
 * @Date 2024/07/28 15:31
 */
@Api
@RequestMapping("/sys/log")
@RestController
public class SysLogController {

    @Resource
    private SysLogService sysLogService;

    /**
     * 加载系统操作日志
     * @param current 当前页
     * @param size 每页显示条数
     * @param userId 用户id
     * @param operation 操作描述
     * @return 分页结果
     */
    @ApiOperation("多条件分页查询系统操作日志")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation){
        Page<SysLog> page = new Page<>(current, size);
        page = sysLogService.page(page, new LambdaQueryWrapper<SysLog>()
                .eq(ObjectUtil.isNotEmpty(userId), SysLog::getUserId, userId)
                .like(ObjectUtil.isNotEmpty(operation), SysLog::getOperation, operation)
        );

        return Result.success(page);
    }
}
